package sfeir.gwt.ergosoom.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sfeir.gwt.ergosoom.client.model.Address;
import sfeir.gwt.ergosoom.client.model.Email;
import sfeir.gwt.ergosoom.client.model.NetworkItem;
import sfeir.gwt.ergosoom.client.model.Person;
import sfeir.gwt.ergosoom.client.model.Tel;
import sfeir.gwt.ergosoom.server.service.ProfileService;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

@SuppressWarnings("serial")
@Singleton
public class VcardProfile extends HttpServlet {
    @Inject
    private Injector injector;

    // @Inject
    // private ProfileService profileService;

    private ProfileService getProfileService() {
        return injector.getInstance(ProfileService.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getPathInfo();
        if (username != null) {
            username = username.substring(1);
        } else {
            username = "";
        }
        Person p = getProfileService().getProfile(username).getPerson();
        resp.setHeader("Content-type", "text/x-vcard");
        PrintWriter str = resp.getWriter();
        str.append("BEGIN:VCARD\r\n");
        str.append("VERSION:3.0\r\n");
        str.append("N:");
        str.append(p.getName());
        str.append(';');
        str.append(p.getFirstname());
        str.append(";;;\r\n");
        str.append("FN:");
        str.append(p.getFullName());
        str.append("\r\n");
        if (p.getNickname() != null) {
            str.append("NICKNAME:");
            str.append(p.getNickname());
            str.append("\r\n");
        }
        if (p.getAboutMe() != null) {
            str.append("NOTE:");
            str.append(p.getAboutMe());
            str.append("\r\n");
        }
        if (p.getCompany() != null) {
            str.append("ORG:");
            str.append(p.getCompany());
            str.append("\r\n");
        }
        if (p.getTitle() != null) {
            str.append("TITLE:");
            str.append(p.getTitle());
            str.append("\r\n");
        }
        if (p.getBirthDay() != null) {
            str.append("BDAY:");
            str.append(p.getBirthDay());
            str.append("\r\n");
        }
        if (p.getPhoto() != null) {
            str.append("URL:");
            str.append(p.getPhoto());
            str.append("\r\n");
        }
        List<Tel> tels = p.getTels();
        for (Tel tel : tels) {
            str.append("TEL");
            switch (tel.getType()) {
            case Tel.HOME_LANDLINE:
                str.append(";TYPE=HOME:");
                break;
            case Tel.MOBILE_LINE:
                str.append(";TYPE=CELL:");
                break;
            case Tel.PROFESSIONNAL_LINE:
                str.append(";TYPE=WORK:");
                break;
            default:
                str.append(':');
                break;
            }
            str.append(tel.getPhoneNumber());
            str.append("\r\n");
        }
        List<Email> emails = p.getEmails();
        for (Email email : emails) {
            str.append("EMAIL");
            switch (email.getTypeEmail()) {
            case Tel.HOME_LANDLINE:
                str.append(";TYPE=HOME:");
                break;
            case Tel.MOBILE_LINE:
                str.append(";TYPE=CELL:");
                break;
            case Tel.PROFESSIONNAL_LINE:
                str.append(";TYPE=WORK:");
                break;
            default:
                str.append(':');
                break;
            }
            str.append(email.getEmail());
            str.append("\r\n");
        }
        List<Address> addresses = p.getAddresses();
        for (Address addresse : addresses) {
            str.append("ADR");
            switch (addresse.getType()) {
            case Address.PERSONAL_ADDRESS:
                str.append(";TYPE=HOME:");
                break;
            case Address.PROFESSIONNAL_ADDRESS:
                str.append(";TYPE=WORK:");
                break;
            default:
                str.append(':');
                break;
            }
            str.append(addresse.getFullValue());
            str.append("\r\n");
        }
        List<NetworkItem> networks = p.getNetworkItems();
        for (NetworkItem network : networks) {
            str.append("URL:");
            str.append(network.getNetworkItemLink());
            str.append("\r\n");
        }
        if (p.getGeo() != null) {
            str.append("GEO:");
            str.append(Double.toString(p.getGeo().getLatitude()));
            str.append(';');
            str.append(Double.toString(p.getGeo().getLongitude()));
            str.append("\r\n");
        }
        str.append("X-GENERATOR:ErgoSoom\r\n");
        str.append("END:VCARD\r\n");

        // TEL;TYPE=HOME:+33149600898
        // TEL;TYPE=CELL:+33675950150
        // GEO:48.868215;2.227306
        // ADR;TYPE=HOME;CHARSET=UTF-8:;;7 rue Anatole France;Le
        // Kremlin-Bicêtre;Val-d
        // e-Marne;94270;France
        // N;CHARSET=UTF-8:Cappelle;Florent;;;
        // FN;CHARSET=UTF-8:Florent Cappelle
        // NICKNAME;CHARSET=UTF-8:Bigneotux
        // NOTE;CHARSET=UTF-8:Get ErgoSoom !
        // X-GENERATOR:telProxy vCard 1.0
        // END:VCARD");
    }
}
